﻿/**
 * JS about calendar
 */
$(document)
		.ready(
				function() {
					var target = $('#calendar');
					target.fullCalendar({
								header : {
									right : '',
									center : '',
									left : ''
								},
								locale:'zh-cn',
								theme : false,
								selectable : true,
								selectHelper : true,
								editable : true,
								events : "calendar!ajaxGetSource.action",

								dayClick : function(date) {
									var isoDate = moment(date).toISOString();

									$('#modal-new-event').modal('show');
									$('#event-name').val('');
									$('#new-event-start').val(isoDate);
									$('#new-event-end').val(isoDate);
								},

								viewRender : function(view) {
									var calendarDate = $("#calendar")
											.fullCalendar('getDate');
									var calendarMonth = calendarDate.month();

									// Set data attribute for header. This is
									// used to switch header images using css
									$('#calendar .fc-toolbar').attr(
											'data-calendar-month',
											calendarMonth);

									// Set title in page header
									$('.block-header-calendar > h2 > span')
											.html(view.title);
								},

								eventClick : function(event, element) {

									$('.edit-event-id').val(event.id);
									$('.edit-event-title').val(event.title);
									$('.edit-event-description').val(
											event.description);
									$(
											'#modal-edit-event input[value='
													+ event.className + ']')
											.prop('checked', true);
									$('#modal-edit-event').modal('show');
								}
							});
					
					//Add new Event
	                $('body').on('click', '#btn-add-event', function(){
	                    var eventTitle = $('#new-event-title').val();
	                    var id = $('#user-name').html() + Date.now();
	                    var cn = $('.event-tag input:checked').val();

	                    if (eventTitle != '') {
	                    	$.ajax({
	            				url:"calendar!ajaxSave.action",
	            				data:{
	            					id: id,
		                            title: eventTitle,
		                            start: $('#new-event-start').val(),
		                            end:  $('#new-event-end').val(),
		                            className: cn
	            				},
	            				type:"post",
	            				dataType:"json",
	            				contentType:"application/x-www-form-urlencoded; charset=utf-8", 
	            				success:function(data){
	            					if (data.flag == 1){
	            						$('#calendar').fullCalendar('renderEvent', {
	        	                            id: id,
	        	                            title: eventTitle,
	        	                            start: $('#new-event-start').val(),
	        	                            end:  $('#new-event-end').val(),
	        	                            allDay: true,
	        	                            className: cn
	        	                        },true );
	            						swal("保存完成!");
	            					}else{
	            						swal("保存失败，请重新尝试!");
	            					}
	            				}
	            			});

	                        $('.form-event')[0].reset();
	                        $('#modal-new-event').modal('hide');
	                        $('#new-event-title').closest('.form-group').removeClass('has-error');
	                    }
	                    else {
	                        $('#new-event-title').closest('.form-group').addClass('has-error');
	                        $('#new-event-title').focus();
	                    }
	                });
	                
					// Update/Delete an Event
					$('body').on(
									'click',
									'[data-calendar]',
									function() {
										var calendarAction = $(this).data(
												'calendar');
										var currentId = $('.edit-event-id')
												.val();
										var currentTitle = $(
												'.edit-event-title').val();
										var currentDesc = $(
												'.edit-event-description')
												.val();
										var currentClass = $(
												'.event-tag-edit input:checked')
												.val();
										var currentEvent = $('#calendar')
												.fullCalendar('clientEvents',
														currentId);
						                
										// Update
										if (calendarAction === 'update') {
											if (currentTitle != '') {
												currentEvent[0].title = currentTitle;
												currentEvent[0].description = currentDesc;
												currentEvent[0].className = currentClass;
												$.ajax({
						            				url:"calendar!ajaxSave.action",
						            				data:{
						            					id: currentId,
				        	                            title: currentTitle,
				        	                            description: currentDesc,
				        	                            className: currentClass
						            				},
						            				type:"post",
						            				dataType:"json",
						            				contentType:"application/x-www-form-urlencoded; charset=utf-8", 
						            				success:function(data){
						            					if (data.flag == 1){
						            						$('#calendar').fullCalendar(
																	'updateEvent',
																	currentEvent[0]);
						            						swal("保存完成!");
						            					}else{
						            						swal("保存失败，请重新尝试!");
						            					}
						            				}
						            			});
												
												$('#modal-edit-event').modal(
														'hide');
											} else {
												$('.edit-event-title').closest(
														'.form-group')
														.addClass('has-error');
												$('.edit-event-title').focus();
											}
										}

										// Delete
										if (calendarAction === 'delete') {
											$('#modal-edit-event')
													.modal('hide');

											setTimeout(
													function() {
														swal(
																{
																	title : '确定删除吗?',
																	text : "此操作不可逆!",
																	type : 'warning',
																	showCancelButton : true,
																	confirmButtonColor : '#3085d6',
																	cancelButtonColor : '#d33',
																	confirmButtonText : '确定'
																})
																.then(
																		function() {
																			$.ajax({
													            				url:"calendar!ajaxDelete.action",
													            				data:{
													            					id: currentId
													            				},
													            				type:"post",
													            				dataType:"json",
													            				contentType:"application/x-www-form-urlencoded; charset=utf-8", 
													            				success:function(data){
													            					if (data.flag == 1){
													            						target.fullCalendar(
																								'removeEvents',
																								currentId);
													            						swal(
													            							'完成',
													            							'该备忘已删除',
																							'success');
													            					}else{
													            						swal("删除失败，请重新尝试!");
													            					}
													            				}
													            			});
																		})
													}, 200);
										}
									});

					// Calendar views switch
					$('body').on('click', '[data-calendar-view]', function(e) {
						e.preventDefault();

						$('[data-calendar-view]').removeClass('active');
						$(this).addClass('active');
						var calendarView = $(this).attr('data-calendar-view');
						target.fullCalendar('changeView', calendarView);
					});

					// Calendar Next
					$('body').on('click', '.calendar-next', function(e) {
						e.preventDefault();
						target.fullCalendar('next');
					});

					// Calendar Prev
					$('body').on('click', '.calendar-prev', function(e) {
						e.preventDefault();
						target.fullCalendar('prev');
					});
				});
